<template>
  <div v-loading="loading" class="autocss"></div>
</template>

  <script>
import loginForm from "@/views/custom/mobiledesign/mobileComponents/loginForm.vue";
import { getTenantIdByName } from "@/api/system/tenant";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import { getTenantEnable } from "@/utils/admin";
import { getStore, setStore } from "@/utils/store";
import Verify from "@/components/verifition/Verify";
import {
  getAccessToken,
  getRefreshToken,
  getTenantId,
  setToken,
  encryption,
} from "@/utils/auth";
import { getHomePage, autoLogin } from "@/api/login";
import { getVerifyCaptcha } from "@/api/system/systemConfig";
import { getQiankunEnv } from "@/utils/micro";

export default {
  name: "Login",
  components: {
    Verify,
    loginForm,
  },
  data() {
    return {
      active: "",
      codeUrl: "",
      captchaEnable: true,
      tenantEnable: true,
      tenantList: [],
      loginForm: {
        username: "",
        password: "",
        code: "",
        uuid: "",
        tenantId: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
      },
      redirect: undefined,
      passwordType: "password",
      loginConfig: {},
      isQiankun: getQiankunEnv(),
      loading: false,
    };
  },
  created() {
    this.loading = true;
    let username = this.$route.query.username;
    if (username) {
      autoLogin(username)
        .then((res) => {
          if (res.code === 0) {
            setToken(res.data);
            this.$store.commit("SET_LOGINPROMPT", res.data.loginPrompt); //登录提示语
            getHomePage()
              .then((res) => {
                this.loading = false;
                if (this.isQiankun && process.env.VUE_APP_NEW_HOME_URL) {
                  // qiankun微前端，适配共享交换，登录后跳转到共享交换首页
                  this.$router
                    .push({ path: process.env.VUE_APP_NEW_HOME_URL })
                    .catch(() => {});
                } else {
                  this.$router.push({ path: res.data.path }).catch(() => {});
                }
              })
              .catch((error) => {
                this.loading = false;
                localStorage.clear();
                console.log(error);
              });

            this.$websocket.initWebSocket(getAccessToken());
          } else {
            this.loading = false;
          }
        })
        .catch((e) => {
          this.loading = false;
        });
    }
    // 租户开关
    this.tenantEnable = getTenantEnable();
    // 设置租户
    this.active = getStore({ name: "tenantId" });
    this.getLoginData();
  },
  methods: {
    getLoginData() {
      this.$store.dispatch("GetLoginConf").then(() => {
        this.loginConfig =
          this.$store.state.user && this.$store.state.user.loginConfigData;
        document.title =
          this.$store.state.user &&
          this.$store.state.user.loginConfigData.loginName;
        var link =
          document.querySelector("link[rel*='icon']") ||
          document.createElement("link");
        link.type = "image/x-icon";
        link.rel = "shortcut icon";
        link.href = this.$store.state.user.loginConfigData.browserUrl;
        document.getElementsByTagName("head")[0].appendChild(link);
      });
    },
  },
};
</script>
  <style scoped>
.autocss {
  width: 100vw;
  height: 100vh;
}
</style>
